package com.gxa.service.community.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.entity.community.Community;
import com.gxa.mapper.community.CommunityMapper;
import com.gxa.service.community.CommunityService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CommunityServiceImpl
 * @Description
 * @Author Mr.YUAN
 * @Date 2022/10/12 9:04
 */
@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Override
    public List<Community> query() {
        List<Community> communities = communityMapper.selectList(null);
        return communities;
    }

    @Override
    public List<Community> query(Integer pageNum, Integer pageSize) {
//        Page<Community> page = PageHelper.startPage(pageNum, pageSize);
        List<Community> communityList = communityMapper.selectList(null);
        return communityList;
    }

    @Override
    public Map<String, Object> queryAll(Community community,Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<Community> page = PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Community> wrapper = new QueryWrapper<>();
        wrapper.like(community.getComNum()!=null,"com_num",community.getComNum())
                .like(community.getComName()!=null,"com_name",community.getComName());
        List<Community> communityList = communityMapper.selectList(wrapper);
        PageInfo<Community> pageInfo = new PageInfo<>(communityList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public void add(Community community) {
        communityMapper.insert(community);
    }

    @Override
    public void deleteById(Integer id) {
        communityMapper.deleteById(id);
    }

    @Override
    public Community queryById(Integer id) {
        Community community = communityMapper.selectById(id);
        return community;
    }

    @Override
    public void update(Community community) {
        communityMapper.updateById(community);
    }

    @Override
    public List<Community> queryByIdOrName(String comNum,String comName) {
//        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Community> wrapper = new QueryWrapper<>();
        List<Community> communityList;
        try{
            if (!StringUtils.isNullOrEmpty(comNum) && !StringUtils.isNullOrEmpty(comName)){
//                wrapper.eq("com_num",comNum).eq("com_name",comName);
                wrapper.like("com_num",comNum).like("com_name",comName);
            }else if (!StringUtils.isNullOrEmpty(comNum) && StringUtils.isNullOrEmpty(comName)){
                wrapper.like("com_num",comNum);
            }else if (StringUtils.isNullOrEmpty(comNum) && !StringUtils.isNullOrEmpty(comName)){
                wrapper.like("com_name",comName);
            }
//            if (!StringUtils.isNullOrEmpty(comNum)){
//                wrapper.eq("com_num",comNum);
////                map.put("com_num",comNum);
//            }else if (!StringUtils.isNullOrEmpty(comName)){
//                wrapper.eq("com_name",comName);
////                map.put("com_name",comName);
//            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            communityList = communityMapper.selectList(wrapper);
            wrapper.clear();
        }
        return communityList;
    }


    @Override
    public List<Community> queryByNum(String communityNum) {
        List<Community> communityList = communityMapper.queryByNum(communityNum);
        return communityList;
    }
}